package com.atguigu.system.service.impl;

import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.system.handler.GuiguException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.atguigu.system.utils.MenuHelper;
import com.atguigu.system.utils.RouterHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //获取所有菜单
        List<SysMenu> sysMenus = baseMapper.selectList(null);
        List<SysMenu> resultList = MenuHelper.buildTree(sysMenus);
        //数据转换要求的数据格式
        return resultList;
    }

    @Override
    public void removeMenuById(String id) {
        //查询当前菜单下是否有子菜单
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuiguException(ResultCodeEnum.FAIL.getCode(), "请先删除子菜单");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<SysMenu> findMenuByRoleId(String roleId) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        //获取所有菜单，status=1
        List<SysMenu> menus = baseMapper.selectList(wrapper);
        //根据roleId查询角色分配过的菜单
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(queryWrapper);
        List<String> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            roleMenuIds.add(roleMenu.getMenuId());
        }
        //赋值isSelect
        for (SysMenu menu : menus) {
            menu.setSelect(roleMenuIds.contains(menu.getId()));
        }
        //转换树形结构
        return MenuHelper.buildTree(menus);
    }

    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", assginMenuVo.getRoleId());
        sysRoleMenuMapper.delete(queryWrapper);
        List<String> menuIdList = assginMenuVo.getMenuIdList();
        for (String menuId : menuIdList) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
    }

    @Override
    public List<RouterVo> getUserMenuList(String id) {
        /**
         * 约定：admin是超级管理员，操作所有内容，userid为1
         */
        List<SysMenu> sysMenus = null;
        if ("1".equals(id)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status", 1);
            wrapper.orderByAsc("sort_value");
            sysMenus = baseMapper.selectList(wrapper);
        } else {
            sysMenus = baseMapper.findMenuListByUserId(id);
        }
        List<SysMenu> trees = MenuHelper.buildTree(sysMenus);
        //转换成前端路由
        return RouterHelper.buildRouters(trees);
    }

    @Override
    public List<String> getUserButtonList(String id) {
        List<SysMenu> sysMenus = null;
        if ("1".equals(id)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status", 1);
            wrapper.orderByAsc("sort_value");
            sysMenus = baseMapper.selectList(wrapper);
        } else {
            sysMenus = baseMapper.findMenuListByUserId(id);
        }
        List<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getType() == 2) {
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
    }
}
